package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetAge;
import com.rjwh.dingdong.client.bean.localbean.AgeData;
import com.rjwh.dingdong.client.bean.localbean.GetAgeListData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.DrawBookCatalogMainActivityModel;
import com.rjwh.dingdong.client.mvp.presenter.IDrawBookCatalogMainActivityPresenter;
import com.rjwh.dingdong.client.mvp.view.DrawBookCatalogMainActivityView;

import java.util.List;

/**
 * Created by Kay on 2016-6-3.
 */
public class DrawBookCatalogMainActivityPresenter extends BasePresenter<DrawBookCatalogMainActivityView> implements IDrawBookCatalogMainActivityPresenter {

    private DrawBookCatalogMainActivityModel model;

    public DrawBookCatalogMainActivityPresenter(Context context, DrawBookCatalogMainActivityView mMvpView) {
        super(context, mMvpView);
        model = new DrawBookCatalogMainActivityModel(this);
    }

    /**
     * 获取年龄段
     */
    @Override
    public void doGetAge() {
        if (isViewAttached()) {
            getMvpView().hideErrorView();
            getMvpView().showLoadingView();
            model.doGetAge();
        }
    }

    /**
     * 请求结果
     *
     * @param businessType
     * @param mAsResult
     * @param isRefresh
     */
    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETAGE:
                    ResGetAge resGetAge = (ResGetAge) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetAge != null) {
                        getMvpView().hideErrorView();
                        GetAgeListData data = resGetAge.getData();
                        if (data != null) {
                            List<AgeData> agelist = data.getAgelist();
                            if (agelist != null) {
                                getMvpView().getAge(agelist);
                                getMvpView().setAgeView(true);
                            }
                        }
                    } else {
                        getMvpView().setAgeView(false);
                        getMvpView().showErrorView();
                    }
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }
}
